package com.yunshang.ysmall.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pingplusplus.util.WxpubOAuth;
import com.yunshang.ysmall.model.CartInfo;
import com.yunshang.ysmall.model.IndexPicInfo;
import com.yunshang.ysmall.model.MemberInfo;
import com.yunshang.ysmall.model.OrderInfo;
import com.yunshang.ysmall.model.OrderItemInfo;
import com.yunshang.ysmall.model.ProductImgList;
import com.yunshang.ysmall.model.ProductInfo;
import com.yunshang.ysmall.service.IndexService;
import com.yunshang.ysmall.service.MemberService;
import com.yunshang.ysmall.service.ProductService;
import com.yunshang.ysmall.util.Constants;

@Controller
@RequestMapping("/main")
public class MainController {

	@Resource
	MemberService memberService;

	@Resource
	IndexService indexService;

	@Resource
	ProductService productService;

	@RequestMapping("/")
	public String main(HttpServletRequest request, Model model) {
		
		Map params = new HashMap();
		params.put("imgStatus", "1");
		//查询所有轮播图
		List<IndexPicInfo> picList = indexService.getIndexPicList(params);
		model.addAttribute("picList", picList);
		params.clear();
		params.put("proStatus", "2");
		params.put("sellStatus", "2");
		params.put("startIndex", 0);
		params.put("pageSize", 8);
		//查询上架商品并且是推荐的商品
		List<ProductInfo> recmdProList = productService.getProductListByMap(params);
		model.addAttribute("recmdProList", recmdProList);
		params.clear();
		params.put("proStatus", "2");
		params.put("startIndex", 0);
		params.put("pageSize", 200);
		//查询上架的商品
		List<ProductInfo> allProList = productService.getProductListByMap(params);
		model.addAttribute("allProList", allProList);
		return "mall/index";
	}
	
	@RequestMapping("/tolist")
	public String toprolist(Integer status, HttpServletRequest request, Model model) {
		Map params = new HashMap();
		params.put("searchName", status);
		params.put("startIndex", 0);
		params.put("pageSize", 10);
		List<ProductInfo> proList = productService.getProductListByMap(params);
		model.addAttribute("proList", proList);
		model.addAttribute("title", "搜索结果");
		return "mall/goodslist";
	}

	@RequestMapping("/list/{type}")
	public String list(@PathVariable("type") String type,String typeId,HttpServletRequest request, Model model) {
		Map params = new HashMap();
		params.put("startIndex", 0);
		params.put("pageSize", 200);
		params.put("proStatus", "2");
		if(typeId != null){
			params.put("typeId", typeId);
			model.addAttribute("tid", Integer.parseInt(typeId));
		}
		String title = "全部商品";
		switch (type) {
		case "all":
			break;
		case "new":
			params.put("sellStatus", 3);
			title = "最新商品";
			break;
		case "hot":
			params.put("sellStatus", 1);
			title = "热销商品";
			break;
		case "rmd":
			params.put("sellStatus", 2);
			title = "推荐商品";
			break;
		default:
			break;
		}
		List<ProductInfo> proList = productService.getProductListByMap(params);
		model.addAttribute("proList", proList);
		model.addAttribute("title", title);
		model.addAttribute("typeList", productService.getTypeList());
		return "mall/goodslist";
	}

	@RequestMapping("/mylist")
	public String mylist(HttpServletRequest request, Model model) {
		if (request.getSession().getAttribute(Constants.LOGIN_MEMBER) == null) {
			return "mall/login";
		}
		Map params = new HashMap();
		params.put("startIndex", 0);
		params.put("pageSize", 10);
		List<ProductInfo> proList = productService.getProductListByMap(params);
		model.addAttribute("proList", proList);
		return "mall/goodslist";
	}

	//根据商品的id来查询商品展示商品详情页面。
	@RequestMapping("/getproduct/{productId}")
	public String getproduct(@PathVariable("productId") String productId, HttpServletRequest request, Model model) {
		//通过商品编号找到对应的商品
		ProductInfo productInfo = productService.getProductById(productId);
		//通过商品编号找到对应的图片
		List<ProductImgList> picList = productService.getImgListByProId(productId);
		System.out.println("picList.size:"+picList.size());
		model.addAttribute("picList", picList);
		model.addAttribute("proInfo", productInfo);
		return "mall/detail";
	}

	
	//显示购物车列表
	@RequestMapping("/tocart")
	public String tocart(HttpServletRequest request, Model model) {
		return "mall/cart";
	}

	//添加商品到购物车.订单详细中含有商品的id和商品的数量
	@ResponseBody
	@RequestMapping("/addCart")
	public String addCart(OrderItemInfo orderItemInfo, HttpServletRequest request, Model model) {
		//获取数量
		System.out.println("transcoun:"+orderItemInfo.getCount());
		//根据商品的id来查询商品信息
		ProductInfo productInfo = productService.getProductById(orderItemInfo.getProductId());
		//订单详细信息的名称
		orderItemInfo.setProName(productInfo.getProductName());
		//订单的图片
		orderItemInfo.setSmallImgPath(productInfo.getSmallImgPath());
		//商品的金额
		orderItemInfo.setTransAmt(productInfo.getPrice());
		//商品的积分
		orderItemInfo.setTransBonus(productInfo.getBonus());
		Map<String,OrderItemInfo> cartMap= null;
		//判断session中的购物车是否存在
		if(request.getSession()
				.getAttribute(Constants.MEMBER_CART) == null){
			cartMap = new HashMap<String,OrderItemInfo>();
		}else{
			cartMap = (Map<String,OrderItemInfo>) request.getSession().getAttribute(Constants.MEMBER_CART);
		}
		//判断购物车中在对应的订单号是否有对应的订单详细信息
		if(cartMap.get(orderItemInfo.getProductId()) != null){
			//取出对应订单号中对应的订单详细
			OrderItemInfo orderItem = cartMap.get(orderItemInfo.getProductId());
			//将当前传入的数量和上一次添加的数量相加
			orderItem.setCount(orderItem.getCount() + orderItemInfo.getCount());
		}else{
			//如果商品编号的值为空，则将对应的商品id作为map的key和对应的订单详情信息添加到集合中
			cartMap.put(orderItemInfo.getProductId(), orderItemInfo);
		}
		//即购物车的数据存入到session中
		request.getSession().setAttribute(Constants.MEMBER_CART, cartMap);
		return "{\"result\":\"success\"}";
	}
	
	//清楚
	@ResponseBody
	@RequestMapping("/delCart")
	public String delCart(String productId, HttpServletRequest request, Model model) {
		System.out.println("productId:"+productId);
		//将session中购物车信息取出来
		Map<String,OrderItemInfo> cartMap = (Map<String,OrderItemInfo>) request.getSession().getAttribute(Constants.MEMBER_CART);
		Map<String,OrderItemInfo> checkedMap = (Map<String,OrderItemInfo>) request.getSession().getAttribute(Constants.CHECKED_CART);
		cartMap.remove(productId);
		checkedMap.remove(productId);
		return "{\"result\":\"success\"}";
	}
	
	//选择购物车中要购买的商品
	@ResponseBody
	@RequestMapping("/checkedcart")
	public String checkedcart(String productId,String action, HttpServletRequest request, Model model) {
		System.out.println("productId:"+productId+" action:"+action);
		//将session中的购物车数据取出来
		Map<String,OrderItemInfo> cartMap = (Map<String,OrderItemInfo>) request.getSession().getAttribute(Constants.MEMBER_CART);
		Map<String,OrderItemInfo> checkedMap= null;
		//判断session中是否存在checkedMap对象
		if(request.getSession()
				.getAttribute(Constants.CHECKED_CART) == null){
			//如果不存在则进行创建
			checkedMap = new HashMap<String,OrderItemInfo>();
		}else{
			//如果存在则将其取出
			checkedMap = (Map<String,OrderItemInfo>) request.getSession().getAttribute(Constants.CHECKED_CART);
		}
		//判断传入过来的商品是什么请求类型
		switch (action) {
		case "add":
			//选择要购买的商品。通过商品id取出对应的商品订单详细信息并添加到checkedMap中
			checkedMap.put(productId, cartMap.get(productId));
			break;
		case "remove":
			checkedMap.remove(productId);
			break;
		case "allchecked":
			//将购物车的数据添加到指定的集合中
			checkedMap.putAll(cartMap);
			break;
		case "allremove":
			checkedMap.clear();
			break;
		default:
			break;
		}
		//将购物车中要购买的商品存入到session中
		request.getSession().setAttribute(Constants.CHECKED_CART, checkedMap);
		return "{\"result\":\"success\"}";
	}

	//清空购物车选中的商品
	@ResponseBody
	@RequestMapping("/clearCart")
	public String clearCart(String productId, HttpServletRequest request, Model model) {
		if (request.getSession().getAttribute(Constants.LOGIN_MEMBER) != null) {
			request.getSession().removeAttribute(Constants.MEMBER_CART);
			return "{\"result\":\"success\"}";
		} else {
			return "{\"result\":\"needtologin\"}";
		}
	}

	
	//后台会员列表查询管理
	@RequestMapping("/member/memberLimit")
	public String memberSearch(MemberInfo memberInfo,Model model){
		Map<String ,Object> params = new HashMap<String ,Object> ();
		if(memberInfo!=null&&!"".equals(memberInfo.getMemberId())){
			params.put("memberId", memberInfo.getMemberId());
		}
		if (memberInfo!=null&&!"".equals(memberInfo.getNickName())) {
			params.put("nickName",memberInfo.getNickName());
		}
		params.put("startIndex", 0);
		params.put("pageSize", 10000);
		model.addAttribute("memberList",memberService.getMemberList(params));
		return "mng/main/product/memberList";
		
	}
	
}
